package com.spzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.user.domain.UserAddress;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.service.IRegionService;
import com.spzx.user.service.IUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService {

    @Autowired
    UserAddressMapper userAddressMapper;

    @Autowired
    IRegionService regionService;

    @Override
    public List<UserAddress> selectUserAddressList() {
        Long userId = SecurityContextHolder.getUserId();
        List<UserAddress> userAddressList = userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId));
        return userAddressList;
    }

    @Override
    public int insertUserAddress(UserAddress userAddress) {
        Long userId = SecurityContextHolder.getUserId();

        String provinceName = regionService.getName(userAddress.getProvinceCode());
        String cityName = regionService.getName(userAddress.getCityCode());
        String districtName = regionService.getName(userAddress.getDistrictCode());
        String fullAddress = provinceName+cityName+districtName+userAddress.getAddress();
        userAddress.setFullAddress(fullAddress);
        userAddress.setUserId(userId);

        //如果当前添加地址是默认地址，那么，需要将这个人的其他地址都设置为不是默认的。
        if(userAddress.getIsDefault() == 1){
            UserAddress userAddressUpdate = new UserAddress();
            userAddressUpdate.setIsDefault(0);
            userAddressMapper.update(userAddressUpdate,
                    new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId,userId));
        }

        return userAddressMapper.insert(userAddress);
    }

    @Override
    public int updateUserAddress(UserAddress userAddress) {
        Long userId = SecurityContextHolder.getUserId();

        String provinceName = regionService.getName(userAddress.getProvinceCode());
        String cityName = regionService.getName(userAddress.getCityCode());
        String districtName = regionService.getName(userAddress.getDistrictCode());
        String fullAddress = provinceName+cityName+districtName+userAddress.getAddress();
        userAddress.setFullAddress(fullAddress);

        //如果当前添加地址是默认地址，那么，需要将这个人的其他地址都设置为不是默认的。
        if(userAddress.getIsDefault() == 1){
            UserAddress userAddressUpdate = new UserAddress();
            userAddressUpdate.setIsDefault(0);
            userAddressMapper.update(userAddressUpdate,
                    new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId,userId));
        }

        return userAddressMapper.updateById(userAddress);
    }

    @Override
    public int removeById(Long id) {
        return userAddressMapper.deleteById(id);
    }
}
